/**
 * This file is part of Picasa Photo Uploader.
 *
 * Picasa Photo Uploader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Picasa Photo Uploader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Picasa Photo Uploader. If not, see <http://www.gnu.org/licenses/>.
 */
package com.android.picasaphotouploader;

import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.os.Handler;
import android.preference.PreferenceManager;
import java.util.concurrent.ExecutorService;

/**
 * Class to observe changes to images table
 * 
 * @author Jan Peter Hooiveld
 */
public class ImageTableObserver extends ContentObserver
{
  /**
   * Main application
   */
  private PicasaPhotoUploader application;

  /**
   *  Queue that handles image uploads
   */
  private ExecutorService queue;

  /**
   * Constructor
   * 
   * @param handler Handler for this class
   * @param application Main application
   * @param queue Queue that handles image uploads
   */
  public ImageTableObserver(Handler handler, PicasaPhotoUploader application, ExecutorService queue)
  {
    super(handler);

    this.application = application;
    this.queue       = queue;
  }

  /**
   * This function is fired when a change occurs on the image table
   *
   * @param selfChange
   */
  @Override
  public void onChange(boolean selfChange)
  {
    // get latest image id
    ImageLatest latestImage = new ImageLatest(application);
    int imageId             = latestImage.getId();

    // if id is -1 it means no record was found or it was a update/delete instead of insert
    if (imageId == -1) {
      return;
    }

    // get image item
    ImageItem item  = latestImage.getLatestItem();

    // if no image item returned abort
    if (item == null) {
      return;
    }

    // get user preferences
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(application.getBaseContext());

    // check if we can connect to internet
    if (!CheckInternet.getInstance().canConnect(application.getBaseContext(), prefs)) {
      return;
    }

    // check if album is set in preferences
    if (prefs.getString("album", "").trim().length() == 0) {
      return;
    }

    // get authentication string
    String auth = getAuthentication(prefs);

    // check if authentication succeeded
    if (auth == null) {
      return;
    }

    // check if albums from preferences does not exist
    if (!albumExists(auth, prefs)) {
      return;
    }

    // add auth to image item and then add it to queue
    item.imageAuth = auth;

    // add item to queue
    queue.execute(new ImageUploader(application.getBaseContext(), queue, item, 0));
  }

  /**
   * Get authentication string from Google
   *
   * @param prefs User preferences
   * @return Google authentication string
   */
  private String getAuthentication(SharedPreferences prefs)
  {
    return new GoogleAuthentication(prefs).getAuthenticationString();
  }

  /**
   * Check if Picasa album stored in user preferences exists
   *
   * @param auth Google authentication string
   * @param prefs User preferences
   * @return If album exists or not
   */
  private boolean albumExists(String auth, SharedPreferences prefs)
  {
    return new AlbumExists(auth, prefs.getString("email", ""), prefs.getString("album", "")).hasAlbum();
  }
}
